//
//  MarqueeView.m
//  HiZhua
//
//  Created by Jpwang on 2017/8/26.
//  Copyright © 2017年 Jpwang. All rights reserved.
//

#import "MarqueeView.h"

@interface MarqueeView () {
    UIScrollView *scrollView;
    float margin;
    float nap;
    float textWidth;
    float fontsize;
    NSString *mText;
    UIColor *mColor;
    
    BOOL isScrolling;
    
    NSTimer *timer;
    
    CGPoint contentoffset;
}

@end

@implementation MarqueeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initData];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initData];
    }
    return self;
}

- (void)initData {
    fontsize = 12;
    margin = 10;
    nap =15;
    mColor = [UIColor whiteColor];
}

- (void)setMargin:(float)Margin {
    margin = Margin;
    if (mText) {
        [self setText:mText];
    }
}

- (void)setNap:(float)Nap {
    nap = Nap;
    if (scrollView) {
        CGRect rect = CGRectMake(Nap, 0, self.frame.size.width-Nap*2, self.frame.size.height);
        [scrollView setFrame:rect];
    }
}

- (void)setTextColor:(UIColor *)color {
    mColor = color;
    for (NSInteger i=0; i<scrollView.subviews.count; i++) {
        if ([scrollView.subviews[i] isKindOfClass:[UILabel class]]) {
            UILabel *lb = scrollView.subviews[i];
            [lb setTextColor:color];
        }
    }
}

- (void)setText:(NSString *)text {
    mText = text;
    UIFont *font = [UIFont systemFontOfSize:fontsize];
    NSDictionary *dic = @{NSFontAttributeName:font};
    NSStringDrawingOptions option = NSStringDrawingUsesLineFragmentOrigin;
    textWidth = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:option attributes:dic context:nil].size.width;
    [self updateView];
    [self startMarquee];
}

- (void)updateView {
    if (!scrollView) {
        scrollView = [[UIScrollView alloc] init];
        [scrollView setScrollEnabled:NO];
        [self addSubview:scrollView];
    }
    CGSize size = self.frame.size;
    scrollView.frame = CGRectMake(nap, 0, size.width-nap*2, size.height);
    for (NSInteger i=scrollView.subviews.count-1; i>=0; i--) {
        [scrollView.subviews[i] removeFromSuperview];
    }
    
    int count = (size.width-nap*2)/textWidth;
    count = count<2?4:count+3;
    for (int i=0; i<count; i++) {
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake((textWidth+margin)*i, 0, textWidth, size.height)];
        [lb setFont:[UIFont systemFontOfSize:fontsize]];
        [lb setText:mText];
        [lb setTextColor:mColor];
        [scrollView addSubview:lb];
    }
    [scrollView setContentSize:CGSizeMake((textWidth+margin)*count, 0)];
}

- (void)startMarquee {
    if (mText && mText.length>0) {
        isScrolling = YES;
        contentoffset = CGPointMake(0, 0);
        [MarqueeView cancelPreviousPerformRequestsWithTarget:self selector:@selector(scroll) object:nil];
        [self performSelector:@selector(scroll) withObject:nil afterDelay:0.3];
    }
}

- (void)stopMarquee {
    isScrolling = NO;
    [scrollView setContentOffset:CGPointMake(0, 0)];
    [MarqueeView cancelPreviousPerformRequestsWithTarget:self selector:@selector(scroll) object:nil];
}

- (void)scroll {
    if (!isScrolling) {
        [scrollView setContentOffset:CGPointMake(0, 0)];
    } else {
        if (contentoffset.x<textWidth+margin) {
            contentoffset.x += 0.5;
            [scrollView setContentOffset:contentoffset animated:NO];
            [self performSelector:@selector(scroll) withObject:nil afterDelay:0.01];
        } else {
            contentoffset.x = 0;
            [scrollView setContentOffset:CGPointMake(0, 0)];
            [self performSelector:@selector(scroll) withObject:nil afterDelay:0.0];
        }
    }
}

@end
